﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import flash.events.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class SlotMachineActionCommand extends Command {

        public var event:SlotMachineEvent;
        public var user:UserModel;
        public var sunModel:SunModel;
        public var languageModel:LanguageModel;
        public var worldModel:WorldModel;

        private static const DELETE:String = "DELETE";

        override public function execute():void{
            var _local1:IRoom;
            var _local2:Object;
            var _local3:Array;
            var _local4:TaskRequestEvent;
            if (this.isSlotmachineRoomFull()){
                this.sunModel.simpleMessage = this.languageModel.getTextByKey("slotmachine_message_full_feedback");
                _local1 = this.getRoomByType(GameConstants.TYPE_BATTERY_ROOM);
                _local1.machine.state = WorldConstants.IDLE;
            } else {
                dispatch(new InfoEvent(EventConstants.UPDATE_ACTOR, this.event.machine_id));
                _local2 = {taskType:GameConstants.PLAY_SLOTMACHINE, itemType:GameConstants.SLOTMACHINE, onResult:this.onResult, onError:this.onError};
                _local3 = [new RequestVO("start_raffle")];
                _local4 = new TaskRequestEvent(EventConstants.SEND_TASK_REQUEST, _local3, _local2);
                dispatch(_local4);
            };
            dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
        }
        private function isSlotmachineRoomFull():Boolean{
            var _local1:IRoom = this.getRoomByType(GameConstants.TYPE_BATTERY_ROOM);
            return ((_local1.rewardItems.length >= GameConstants.MAX_ITEMS_BATTERY_ROOM));
        }
        private function onResult(_arg1:ResultVO):void{
            var _local4:Object;
            var _local5:Vector.<RewardItemVO>;
            var _local6:Vector.<RewardItemVO>;
            var _local7:Array;
            var _local2:IRoom = this.getRoomByType(GameConstants.TYPE_BATTERY_ROOM);
            var _local3:Vector.<RewardItemVO> = _local2.rewardItems;
            if (((_arg1.success) && (_arg1.data))){
                _local4 = _arg1.data.start_raffle;
                _local5 = this.createRewardItemsVO((_local4.items as Array));
                _local6 = this.getAddedRewardItems(_local3, _local5);
                _local7 = this.getAllItems(_local3, (_local4.items as Array));
                dispatch(new InfoEvent(EventConstants.SLOTMACHINE_ITEMS_RECEIVED, null, {addedItems:_local6, allItems:_local7}));
                dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, _local4));
            } else {
                dispatch(new InfoEvent(EventConstants.SLOTMACHINE_ITEMS_RECEIVED, null, {addedItems:null, allItems:_local3}));
            };
        }
        private function onError(_arg1:String, _arg2:ResultVO):void{
            var _local3:IRoom = this.getRoomByType(GameConstants.TYPE_BATTERY_ROOM);
            var _local4:Vector.<RewardItemVO> = _local3.rewardItems;
            dispatch(new InfoEvent(EventConstants.SLOTMACHINE_ITEMS_RECEIVED, null, {addedItems:null, allItems:_local4}));
        }
        private function getAllItems(_arg1:Vector.<RewardItemVO>, _arg2:Array):Array{
            var _local5:RewardItemVO;
            var _local3:Array = _arg2;
            var _local4:int = (_local3.length - 1);
            while (_local4 >= 0) {
                for each (_local5 in _arg1) {
                    if (((_local5.alreadyCollected) && ((_local3[_local4].id == _local5.id)))){
                        _local3.splice(_local4, 1);
                    };
                };
                _local4--;
            };
            return (_local3);
        }
        private function getAddedRewardItems(_arg1:Vector.<RewardItemVO>, _arg2:Vector.<RewardItemVO>):Vector.<RewardItemVO>{
            var _local4:RewardItemVO;
            var _local5:RewardItemVO;
            var _local6:int;
            var _local3:Vector.<RewardItemVO> = _arg2.concat();
            for each (_local4 in _arg1) {
                for each (_local5 in _local3) {
                    if (_local5.id == _local4.id){
                        _local6 = _local3.indexOf(_local5);
                        _local3[_local6].itemType = DELETE;
                    };
                };
            };
            return (_local3.filter(this.deleteItems));
        }
        private function deleteItems(_arg1:RewardItemVO, _arg2:int, _arg3:Vector.<RewardItemVO>):Boolean{
            return (!((_arg1.itemType == DELETE)));
        }
        private function getRoomByType(_arg1:String):IRoom{
            var _local2:String = this.worldModel.getRoomIdsOfType(_arg1)[0];
            var _local3:IRoom = (this.worldModel.getRoomById(_local2) as IRoom);
            return (_local3);
        }
        private function createRewardItemsVO(_arg1:Array):Vector.<RewardItemVO>{
            var _local4:Object;
            var _local5:RewardItemVO;
            var _local2:Array = _arg1;
            var _local3:Vector.<RewardItemVO> = new Vector.<RewardItemVO>();
            if (((_local2) && (_local2.length))){
                for each (_local4 in _local2) {
                    _local5 = new RewardItemVO();
                    _local5.id = _local4.id;
                    _local5.itemType = _local4.item_type;
                    _local5.amount = _local4.amount;
                    _local3.push(_local5);
                };
            };
            return (_local3);
        }

    }
}//package net.wooga.pets.controllers 
